<?php
/**
 * 简历解析 Logic
 */

namespace app\common\queue;

use think\cache\driver\Redis;

/**
 * 处理压缩包队列
 * Class ZipQueue
 * @package app\common\logic
 */
class Queue extends \Redis{
    protected static $inition;
    private $select = 15;
    private $config;
    /**
     * @var \Redis
     */
    private $redis;
    public $key;

    public function __construct($config=[])
    {
        $this->config = config('cache');
        $this->config['select'] = $this->select;
        $this->config += $config;

        $this->connect($this->config['host']);

        if($this->config['password'])
            $this->auth($this->config['password']);
        $this->key =  $this->config['prefix'].'_' . ($config['queue_key']??$this->key);

    }

    public function run(){
        $this->setOption(self::OPT_READ_TIMEOUT, -1);
//        $time = date('Y-m-d H:i:s');
        while(true){
            $res = $this->blPop($this->key,50);
            cache('');// 在队列  或常驻系统中 要保持redis连接更新
            if(!$res || empty($res[1])){
                continue;
            }
            echo date('Y-m-d H:i:s').'==='.PHP_EOL;
            $data = json_decode($res[1],1);
            $this->handle($data);
            echo 'handleSuccess=================='.PHP_EOL;
        }
    }
    public function action($params){

        $this->rPush($this->key,json_encode($params,256));
        return 1;
    }
    public function handle($data){
        try {
            dolog(__METHOD__,$data);
            $res = call_user_func([$data['controller'],$data['action']],$data);
            dolog($this->key.':JobsHandle',[$res,$data]);
        }catch (\Exception $e){
            dolog($this->key.'JobsError',[$e->getMessage(),$e->getFile(),$e->getLine()]);
        }
    }
}